package com.lzx.iteam.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.lzx.iteam.R;

/**
 * Created by hfx_lk on 15/3/18.
 */
public class TwiceTextChoiceView extends FrameLayout implements View.OnClickListener {

    TextView mTvAll,mTvSelected;
    private int mState = 0;
    OnChoiceChangeListener mListener;

    public TwiceTextChoiceView(Context context,AttributeSet attrs){
        super(context,attrs);
        LayoutInflater.from(context).inflate(R.layout.twice_text_choice_layout, this);
        mTvAll = (TextView) findViewById(R.id.tv_all);
        mTvSelected = (TextView) findViewById(R.id.tv_selected);
        mTvAll.setOnClickListener(this);
        mTvSelected.setOnClickListener(this);
    }
    
    public void setText(String text)
    {
    	mTvSelected.setText(text);
    }
    
    

    private void setSelectedState(int flag){
       if(flag == 1){
           setAllToPressedState();
       }else if(flag == 2){
           setSelectedToPressedState();
       }else{
       }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_all:
                if(mState != 0){
                    setAllToPressedState();
                    mState = 0;
                    //listener
                    if(mListener != null){
                        mListener.choiceAll();
                    }
                }
                break;
            case R.id.tv_selected:
                if(mState != 1){
                    setSelectedToPressedState();
                    mState = 1;
                    //listener
                    if(mListener != null){
                        mListener.choiceSelected();
                    }
                }
                break;
        }
    }

    public void setAllToPressedState(){
        mTvAll.setBackgroundColor(getResources().getColor(R.color.twice_text_view_selected));
        mTvAll.setTextColor(Color.WHITE);
        mTvSelected.setBackgroundColor(getResources().getColor(R.color.twice_text_view_unselected));
        mTvSelected.setTextColor(Color.BLACK);
        
        mState = 0;
    }

    private void setSelectedToPressedState(){
        mTvAll.setBackgroundColor(getResources().getColor(R.color.twice_text_view_unselected));
        mTvAll.setTextColor(Color.BLACK);
        mTvSelected.setBackgroundColor(getResources().getColor(R.color.twice_text_view_selected));
        mTvSelected.setTextColor(Color.WHITE);
    }

    public void backToDefaultState(){
        mTvAll.setBackgroundColor(getResources().getColor(R.color.twice_text_view_selected));
        mTvAll.setTextColor(Color.WHITE);
        mTvSelected.setBackgroundColor(getResources().getColor(R.color.twice_text_view_unselected));
        mTvSelected.setTextColor(Color.BLACK);
        mState = 0;
    }

    public interface OnChoiceChangeListener{
        public void choiceAll();
        public void choiceSelected();
    }

    public void setOnChoiceChangeListener(OnChoiceChangeListener listener){
        mListener = listener;
    }
}
